package com.pro.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pro.beans.Grid;
import com.pro.beans.GridPersons;
import com.pro.beans.News;
import com.pro.beans.Person;
import com.pro.param.ResponseParam;
import com.pro.service.GridService;

@Controller
public class GridController extends BaseController{

	@Resource
	public GridService gridService;
	@RequestMapping("addGrid.do")
	public String addGrid(@ModelAttribute("grid")Grid grid,Model model,HttpServletRequest request){
//		responseParam
		gridService.addGrid(grid);
		model.addAttribute("msg", "添加成功");
		model.addAttribute("menuId", "3");
		model.addAttribute("jspName", "addGrid");
		request.setAttribute("menuId", "3");
		return "index";
	}
	@RequestMapping("addPerson.do")
	public String addPerson(@ModelAttribute("person")Person person,Model model,HttpServletRequest request){
//		responseParam
		gridService.addPerson(person);
		model.addAttribute("msg", "添加成功");
		model.addAttribute("menuId", "3");
		model.addAttribute("jspName", "addFirePerson");
		request.setAttribute("menuId", "3");
		return "index";
	}
	@RequestMapping("delGrid.do")
	public String delGrid(@RequestParam("id")Integer id,Model model){
//		responseParam
		gridService.delGrid(id);
		model.addAttribute("msg", "删除成功");
		model.addAttribute("menuId", "3");
		model.addAttribute("jspName", "addGrid");
		return "index";
	}
	@RequestMapping("delPerson.json")
	public String delPerson(@RequestParam("id")Integer id,Model model){
		gridService.delPerson(id);
		model.addAttribute("msg", "删除成功");
		model.addAttribute("menuId", "3");
		model.addAttribute("jspName", "addPerson");
		return "index";
	}
	@RequestMapping("loadAllNews.json")
	@ResponseBody
	public ResponseParam<Grid> loadAllNews(){
//		gridService.
		Map<Integer,List<News>> map = gridService.loadNews();
		this.setMsg("删除成功");
		this.setSuccess(true);
		this.setMap(map);
		return this.getResponseParam();
	}
	@RequestMapping("loadAllGrid.json")
	@ResponseBody
	public ResponseParam<Grid> loadAllGrid(){
//		gridService.
		List<Grid> gridList = gridService.loadAllGrid();
		this.setSuccess(true);
		this.setDataList(gridList);
		return this.getResponseParam();
	}
	@RequestMapping("loadNewById.json")
	@ResponseBody
	public ResponseParam<News> loadNewById(@RequestParam("id")Integer id,Model model){
		News news = gridService.loadNewById(id);
		this.setData(news);
		return this.getResponseParam();
	}
	@RequestMapping("loadGridPerson.json")
	@ResponseBody
	public ResponseParam<News> loadGridPerson(@RequestParam("id")Integer id){
		List<GridPersons> firePersonList = gridService.loadFirePersonInGrid(id);
		List<GridPersons> dutyPersonList   = gridService.loadDutyPersonInGrid(id);
		Grid grid =  gridService.loadGridById(id);
		this.setDataList(firePersonList);
		this.setDataList1(dutyPersonList);
		this.setData(grid);
		this.setSuccess(true);
		return this.getResponseParam();
	}
	@RequestMapping("loadGridById.json")
	@ResponseBody
	public ResponseParam<Grid> loadGridById(@RequestParam("id")Integer id){
		this.setData(gridService.loadGridById(id));
		this.setSuccess(true);
		return this.getResponseParam();
	}
}
